package pers.zxk.jlytnews.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    public static String dateFormatLong(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(date);
    }

    public static String dateFormatLongCh(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        return dateFormat.format(date);
    }

    public static String dateFormatShort(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd");
        return dateFormat.format(date);
    }

    public static String dateFormatShortCh(Date date) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM月dd日");
        return dateFormat.format(date);
    }

    public static String dateFormatFull(Date date){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateFormat.format(date);
    }

    /**
     * 得到本周周一
     *
     * @return yyyy-MM-dd
     */
    public static Date getMondayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 1);
        return c.getTime();
    }

    /**
     * 得到本周周日
     *
     * @return yyyy-MM-dd
     */
    public static Date getSundayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return c.getTime();
    }

    /**
     * 增加日期
     * @param date 当前日期
     * @param count 增加天数 可以负值
     * @return
     */
    public static Date addDate(Date date, int count) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, count);
        return c.getTime();
    }

    /**
     * 判断日期是否是今天
     * @param date
     * @return
     */
    public static boolean isToday(Date date){
        String today = dateFormatLong(new Date());
        return today.equals(dateFormatLong(date));
    }


    public static Date int2Date(int date) {
        Long timestamp = Long.valueOf(date) * 1000L; //*1000为了转换为毫秒
        return new Date(Long.valueOf(timestamp));
    }

    public static int date2Int(Date date){
        long time = date.getTime();//long：1970-01-01 00:00:00到当前时间的毫秒值
        /*
        为啥要转为int类型？
        节省内存，因为↓
        时间（2038-01-19 11:14:07）以前的日期转为long类型后小于2147483647000，除以1000（即舍弃毫秒值）刚好可以用整型表示
         */
        int intTime = (int) (time/1000);//转换为秒
        return intTime;
    }
}
